<?php

namespace App\Http\Controllers\Admin\Env;

use App\Exceptions\LogicException;
use App\Http\Controllers\Controller;
use App\Models\Adminer\Admins;
use App\Models\Env\CommonHeader;
use App\Models\Projects\Product;
use App\Utils\QueryUtil;
use App\Utils\ResUtil;

class CommonHeaderController extends Controller
{
    public function list()
    {
        $request = request()->all();
        $query = CommonHeader::where(function($query) use($request) {
            QueryUtil::selectBuild($query, $request, [
                ['product_id', '='],
                ['key', 'like', 'key'],
                ['enable', '='],
                ['remark', 'like', 'remark'],
                ['created_adminer', 'like', 'created_adminer'],
                ['updated_adminer', 'like', 'updated_adminer'],
                ['id', '='],
            ]);
        });

        $total = $query->count();
        $models = QueryUtil::queryList($query);
        $list = [];
        foreach($models as $i => $v) {
            $list[$i] = $v?->label()?->toArray();
        }
        $hasMore = QueryUtil::hasMore(sizeof($list));
        return $this->ok(compact('list', 'total', 'hasMore'));
    }

    public function edit()
    {
        $data = request()->all();
        $rule = [
            'id' => '',
            'product_id' => 'required',
            'key' =>  'string|required',
            'value' => 'string|required',
            'remark' => 'required',
            'enable' => '',
        ];
        $validator = validatorService()->checkValidator($rule, $data, CommonHeader::$fillables);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }

        $product = Product::find($data['product_id']);
        if(!$product) {
            throw new LogicException('产品不存在');
        }

        // 判断名称不能重复
        $exists = CommonHeader::where('key', $data['key'])->where('product_id', $data['product_id'])->first();
        if($exists && ($exists->id !== ($data['id']??null))) {
            throw new LogicException('该产品下已经存在相同名称key的请求头');
        }

        if($data['id'] ?? '') {
            $CommonHeader = CommonHeader::find($data['id']);
            if(!$CommonHeader) {
                throw new LogicException(ResUtil::NOT_DATA);
            }
        } else {
            $CommonHeader = new CommonHeader();
            $CommonHeader->enable = 1;
            $CommonHeader->created_adminer = Admins::getInfoById($this->authAdmin()->id);
        }

        $CommonHeader->fill([
            'product_id' => $product->id,
            'key' => $data['key'],
            'value' => $data['value'],
            'remark' => $data['remark'] ?? '',
            'enable' => $data['enable'] ?? 1,
            'updated_adminer' => Admins::getInfoById($this->authAdmin()->id),
        ]);
        $CommonHeader->save();
        return $this->ok();
    }

    public function enable()
    {
        $data = request()->all();
        $rule = [
            'id' => 'integer|required',
            'enable' => 'integer|required|in:1,0',
        ];
        $validator = validatorService()->checkValidator($rule, $data, CommonHeader::$fillables);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }
        $Host = CommonHeader::find($data['id']);
        if(!$Host) {
            throw new LogicException(ResUtil::NOT_DATA);
        }

        $Host->enable = $data['enable'];
        $Host->save();
        return $this->ok();
    }

    public function allCommonHeader()
    {
        $list = CommonHeader::select('id', 'key')->get();
        return $this->ok(compact('list'));
    }
}
